/**
 * @author zlss
 * @date 2025/5/31
 * @description
 */
import java.io.Serializable;

public class SudokuBoard implements Serializable {
    private int[][] board;
    private boolean[][] editable;

    public SudokuBoard() {
        board = new int[9][9];
        editable = new boolean[9][9];
    }

    public void setBoard(int[][] newBoard) {
        for (int row = 0; row < 9; row++) {
            for (int col = 0; col < 9; col++) {
                board[row][col] = newBoard[row][col];
                editable[row][col] = (newBoard[row][col] == 0);
            }
        }
    }

    public int getValue(int row, int col) {
        return board[row][col];
    }

    public void setValue(int row, int col, int value) {
        board[row][col] = value;
    }

    public boolean isEditable(int row, int col) {
        return editable[row][col];
    }

    public boolean isValidMove(int row, int col, int number) {
        // 检查行
        for (int c = 0; c < 9; c++) {
            if (board[row][c] == number) {
                return false;
            }
        }

        // 检查列
        for (int r = 0; r < 9; r++) {
            if (board[r][col] == number) {
                return false;
            }
        }

        // 检查3x3宫格
        int boxRow = row / 3 * 3;
        int boxCol = col / 3 * 3;
        for (int r = boxRow; r < boxRow + 3; r++) {
            for (int c = boxCol; c < boxCol + 3; c++) {
                if (board[r][c] == number) {
                    return false;
                }
            }
        }

        return true;
    }

    public boolean isComplete() {
        for (int row = 0; row < 9; row++) {
            for (int col = 0; col < 9; col++) {
                if (board[row][col] == 0) {
                    return false;
                }
            }
        }
        return true;
    }

    public boolean isValid() {
        // 检查每一行
        for (int row = 0; row < 9; row++) {
            boolean[] used = new boolean[10];
            for (int col = 0; col < 9; col++) {
                int num = board[row][col];
                if (num != 0 && used[num]) {
                    return false;
                }
                used[num] = true;
            }
        }

        // 检查每一列
        for (int col = 0; col < 9; col++) {
            boolean[] used = new boolean[10];
            for (int row = 0; row < 9; row++) {
                int num = board[row][col];
                if (num != 0 && used[num]) {
                    return false;
                }
                used[num] = true;
            }
        }

        // 检查每个3x3宫格
        for (int boxRow = 0; boxRow < 9; boxRow += 3) {
            for (int boxCol = 0; boxCol < 9; boxCol += 3) {
                boolean[] used = new boolean[10];
                for (int row = boxRow; row < boxRow + 3; row++) {
                    for (int col = boxCol; col < boxCol + 3; col++) {
                        int num = board[row][col];
                        if (num != 0 && used[num]) {
                            return false;
                        }
                        used[num] = true;
                    }
                }
            }
        }

        return true;
    }

    public void printBoard() {
        for (int row = 0; row < 9; row++) {
            if (row % 3 == 0) {
                System.out.println("-------------------------");
            }
            for (int col = 0; col < 9; col++) {
                if (col % 3 == 0) {
                    System.out.print("| ");
                }
                System.out.print(board[row][col] + " ");
            }
            System.out.println("|");
        }
        System.out.println("-------------------------");
    }
}

